<script>
export  default {
  name:"GoodsManage",
  data() {
    let checkCount = (rule, value, callback) => {
      if(value>9999){
        callback(new Error('数量输⼊过⼤'));
      }else{
        callback();
      }
    };
    return {
      goodstypeData:[],
      // kindData:[],
      supplierData:[],
      tableData: [],
      pageSize:5,
      pageNum:1,
      total:0,
      category: '',
      // kind :'',
      supplier:'',
      name: '',
      centerDialogVisible:false,
      form:{
        productId:0,
        productName:'',
        categoryId:0,
        price: 0,
        supplierId:0,
        stock:0,
        description:'',
        imageUrl:'',
        createTime:'',
        updateTime:'',
        videoUrl:'',
        sold:0,
        type:0,
        click:0,
      },
      rules: {
        name: [
          { required: true, message: '请输入物品名', trigger: 'blur' },
        ],
        category:[
          { required: true, message: '请输入分类', trigger: 'blur' },
        ],
        // kind:[
        //   { required: true, message: '请输入种类', trigger: 'blur' },
        // ],
        supplier:[
          { required: true, message: '请输入供货商', trigger: 'blur' },
        ],
        count: [
          {required: true, message: '请输⼊数量', trigger: 'blur'},
          {pattern: /^([1-9][0-9]*){1,4}$/,message: '数量必须为正整数字',trigger: "blur"},
          {validator:checkCount,trigger: 'blur'}
        ],

      }
    }
  },
  methods:{
    formatGoodstype(row){
      console.log(this.goodstypeData)
      let temp = this.goodstypeData.find(item=>{
        return item.categoryId == row.categoryId
      })
      return temp && temp.categoryName
    },
    // formatKind(row){
    //   let temp = this.kindData.find(item=>{
    //     return item.id == row.kind
    //   })
    //   return temp && temp.name
    // },
    formatSupplier(row){
      let temp = this.supplierData.find(item=>{
        return item.supplierId == row.supplierId
      })
      return temp && temp.supplierName
    },
    loadPost(){
      this.$axios.post(this.$httpUrl+'/product/listPage',{
        pageSize:this.pageSize,
        pageNum:this.pageNum,
        param:{
          name:this.name,
          categoryId:this.category,
          // kind :this.kind+'',
        }
      }).then(res=>res.data).then(res=> {
        console.log(res)
        if(res.code==200){
          this.tableData=res.data
          this.total=res.total;
          console.log(res.data)
        }else{
          alert('获取数据失败')
        }
        //this.tableData=res
      })
    },
    loadGoodstype(){
      this.$axios.get(this.$httpUrl+'/category/list').then(res=>res.data).then(res=> {
        console.log(res)
        if(res.code==200){
          this.goodstypeData=res.data
        }else{
          alert('获取数据失败')
        }
        //this.tableData=res
      })
    },
    // loadKind(){
    //   this.$axios.get(this.$httpUrl+'/kind/list').then(res=>res.data).then(res=> {
    //     console.log(res)
    //     if(res.code==200){
    //       this.kindData=res.data
    //     }else{
    //       alert('获取数据失败')
    //     }
    //     //this.tableData=res
    //   })
    // },
    loadSupplier(){
      this.$axios.get(this.$httpUrl+'/supplier/list').then(res=>res.data).then(res=> {
        console.log(res)
        if(res.code==200){
          this.supplierData=res.data
        }else{
          alert('获取数据失败')
        }
        //this.tableData=res
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    resetParam(){
      this.name=''
      // this.kind=''
      this.category=''
      this.supplier=''
    },
    add(){
      this.centerDialogVisible = true
      this.$nextTick(()=>{
        this.resetForm();
        this.form.id = ''
      })
    },
    save(){
      // this.$refs.form.validate((valid) => {
      //   if (valid) {
          if(this.form.productId){
            this.doMod();
          }else {
            this.doSave();
          }
      //   } else {
      //     console.log('error submit!!');
      //     return false;
      //   }
      // });
    },
    doSave(){
      this.$axios.post(this.$httpUrl+'/product/save',this.form).then(res=>res.data).then(res=> {
        console.log(res)
        if(res.code==200){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible = false
          this.loadPost()
          this.resetForm()
        }else{
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
        //this.tableData=res
      })
    },
    doMod(){
      this.$axios.post(this.$httpUrl+'/product/update',this.form).then(res=>res.data).then(res=> {
        console.log(res)
        if(res.code==200){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible = false
          this.loadPost()
        }else{
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
        //this.tableData=res
      })
    },
    resetForm(){
      this.$refs.form.resetFields();
    },
    del(id){
      console.log(id)
      this.$axios.get(this.$httpUrl+'/product/del?id='+id).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code == 200){
          this.$message({
            message:'操作成功！',
            type:'success'
          });
          //this.centerDialogVisible=false
          this.loadPost()
          //this.resetForm()
        }
        else{
          this.$message({
            message: '操作失败！',
            type: 'error'
          });
        }
      })
    },
    mod(row){
      console.log(row)
      this.centerDialogVisible = true
      let selectedProduct = this.tableData.find(item=>{
        return item.productId == row.productId
      })
      console.log("selectedProduct",selectedProduct)

      this.$nextTick(()=>{
        this.form = selectedProduct;
        // //赋值到表单
        // this.form.productId = row.productId
        // this.form.productName = row.productName
        // this.form.categoryId = row.categoryId
        // // this.form.kind = row.kind
        // this.form.price = row.price
        // this.form.supplierId = row.supplierId
        // this.form.stock = row.stock
        // this.form.description = row.description
      })
    }
  },
  beforeMount() {
    this.loadGoodstype()
    // this.loadKind()
    this.loadSupplier();
    this.loadPost();

  }
}
</script>

<template>
  <div>
    <div style="margin-bottom: 5px">
      <el-input v-model="name" placeholder="请输入物品名" suffix-icon="el-icon-search" style="width: 200px" @keyup.enter.native="loadPost"></el-input>
      <el-select v-model="category" placeholder="请选择分类">
        <el-option
            v-for="item in goodstypeData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
<!--      <el-select v-model="kind" placeholder="请选择种类">-->
<!--        <el-option-->
<!--            v-for="item in kindData"-->
<!--            :key="item.id"-->
<!--            :label="item.name"-->
<!--            :value="item.id">-->
<!--        </el-option>-->
<!--      </el-select>-->
      <el-button type="primary" style="margin-left: 5px" @click="loadPost">查询</el-button>
      <el-button type="success" style="margin-left: 5px" @click="resetParam">重置</el-button>
      <el-button type="success" style="margin-left: 5px" @click="add">新增</el-button>
    </div>
    <el-table :data="tableData"
              :header-cell-style="{ background: '#f2f5fc', color: '#555555'}"
              border
    >
      <el-table-column prop="productId" label="ID" align="center" width="50px">
      </el-table-column>
      <el-table-column prop="productName" label="物品名" align="center">
      </el-table-column>
      <el-table-column prop="categoryId" label="分类" align="center" :formatter="formatGoodstype">
      </el-table-column>
<!--      <el-table-column prop="kind" label="种类" align="center" :formatter="formatKind">-->
<!--      </el-table-column>-->
      <el-table-column prop="price" label="价格" align="center">
      </el-table-column>
      <el-table-column prop="supplierId" label="供货商" align="center" :formatter="formatSupplier">
      </el-table-column>
<!--      <el-table-column prop="count" label="数量" align="center">-->
<!--      </el-table-column>-->
      <el-table-column prop="description" label="描述" align="center">
      </el-table-column>


      <el-table-column label="操作" align="center">
        <template v-slot="scope">
          <el-button type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="确定删除吗？"
              @confirm="del(scope.row.id)"
              style="margin-left: 5px;"
          >
            <el-button slot="reference"  type="danger" >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        title="提示"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="物品名" prop="name">
          <el-col :span="20">
            <el-input v-model="form.productName"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="分类" prop="category">
          <el-col :span="20">

            <el-select v-model="form.categoryId" placeholder="请选择分类">
              <el-option
                  v-for="item in goodstypeData"
                  :key="item.categoryId"
                  :label="item.categoryName"
                  :value="item.categoryId">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
<!--        <el-form-item label="种类" prop="kind">-->
<!--          <el-col :span="20">-->

<!--            <el-select v-model="form.kind" placeholder="请选择种类">-->
<!--              <el-option-->
<!--                  v-for="item in kindData"-->
<!--                  :key="item.id"-->
<!--                  :label="item.name"-->
<!--                  :value="item.id">-->
<!--              </el-option>-->
<!--            </el-select>-->
<!--          </el-col>-->
<!--        </el-form-item>-->
        <el-form-item label="价格" prop="price">
          <el-col :span="20">
            <el-input v-model="form.price"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="供货商" prop="supplier">
          <el-col :span="20">

            <el-select v-model="form.supplierId" placeholder="请选择供货商">
              <el-option
                  v-for="item in supplierData"
                  :key="item.supplierId"
                  :label="item.supplierName"
                  :value="item.supplierId">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="数量" prop="count">
          <el-col :span="20">
            <el-input v-model="form.stock"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="" prop="remark">
          <el-col :span="20">
            <el-input type="textarea" v-model="form.description"></el-input>
          </el-col>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>